{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 矩阵克隆"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "等号赋值相当于起别名，通过别名修改矩阵，原矩阵也会改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(tensor([[  1,   2,   3],\n",
       "         [  4,   5, 100]]),\n",
       " tensor([[  1,   2,   3],\n",
       "         [  4,   5, 100]]))"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = torch.tensor([[1,2,3],[4,5,6]])\n",
    "B = A\n",
    "\n",
    "print(id(B)==id(A))\n",
    "B[1,2]=100\n",
    "B,A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用`clone`函数，才会另外创建一个相同的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(tensor([[  1,   2,   3],\n",
       "         [  4,   5, 100]]),\n",
       " tensor([[1, 2, 3],\n",
       "         [4, 5, 6]]))"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = torch.tensor([[1,2,3],[4,5,6]])\n",
    "B = A.clone()\n",
    "\n",
    "print(id(B)==id(A))\n",
    "B[1,2]=100\n",
    "B,A"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 求最小值（最大值/平均值/求和）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "以`shape=(2,2,3)`的矩阵为例\n",
    "\n",
    "<img src=\"https://zyc-learning-1309954661.cos.ap-nanjing.myqcloud.com/machine-learning-pic/1712843202154-screenshot.png\" alt=\"1712843202154-screenshot\" style=\"zoom:67%;\" />"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[3, 3, 4],\n",
       "         [3, 1, 0]]),\n",
       " tensor([[3, 3, 1],\n",
       "         [3, 1, 0]]),\n",
       " tensor([[3, 1],\n",
       "         [3, 0]]))"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([[[3,6,4],[3,3,1]],[[4,3,6],[3,1,0]]])\n",
    "x.min(dim=0).values, x.min(dim=1).values, x.min(dim=2).values"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "把`(2,2,3)`矩阵想象成2个2*3的矩阵上下堆叠起来。\n",
    "\n",
    "- `dim=0`指的是第一个‘2’，表示有2个2\\*3矩阵，第0个2\\*3矩阵在上，第1个在下，从编号小的往大的方向看；\n",
    "- `dim=1`指的是第二个‘2’，按二维矩阵行递增的方向看\n",
    "- `dim=2`指的是‘3’，按二维矩阵列递增的方向看"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`dim`还可以取值为一个列表（只适用于`mean`和`sum`）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([13., 13., 11.])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([[[3,6,4],[3,3,1]],[[4,3,6],[3,1,0]]],dtype=torch.float32)\n",
    "x.sum(dim=[0,1])        # 先按图中dim=0的方向相加，再按dim=1的方向相加"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
